package threadBaseUse;

/**
 * @Author: WanqingLiu
 * @Date: 2023/02/02/14:20
 * 检测线程状态
 * */
public class ThreadState {

    public static void main(String[] args) throws InterruptedException {


        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        // 观察状态
        Thread.State state = thread.getState();
        System.out.println(state);

        // 观察启动后
        thread.start();
        state = thread.getState();
        System.out.println(state);

        // 不终止一直打印状态
        while (state != Thread.State.TERMINATED){
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);
        }

    }

}
